import { Controller, AppEvent } from "../../framework/framework";
import { CartonModel } from '../models/model';
import { CartonView } from '../views/view';

class CartonController extends Controller implements IController {
    private _model: IModel;
    private _view: IView;

    constructor(metiator: IMediator) {
        super(metiator);
        this._model = new CartonModel(metiator);
        this._view = new CartonView(metiator);
    }

    // initialize views/ models and strat listening to controller actions
    public initialize(): void {
        // subscribe to controller action events
        this.subscribeToEvents([
            new AppEvent("app.controller.carton.news_list", null, (e, symbol: string) => { this.news_list(symbol); }),
            new AppEvent("app.controller.carton.news_add", null, (e, symbol: string) => { this.news_add(symbol); }),
            new AppEvent("app.controller.carton.news_has", null, (e, symbol: string) => { this.news_has(symbol); }),
            new AppEvent("app.controller.carton.user_login", null, (e, symbol: string) => { this.user_login(symbol); })        
        ]);

        // initialize view and models events
        this._model.initialize();
        this._view.initialize();
    }

    // dispose views/models and stop listening to controller actions
    public dispose(): void {

        // dispose the controller events
        this.unsubscribeToEvents();

        // dispose views and model events
        this._model.dispose();
        this._view.dispose();
    }

    public news_list(args: any) {
        this.triggerEvent(new AppEvent("app.model.carton.news_list", args, null));
    }
    public news_add(args: any) {
        this.triggerEvent(new AppEvent("app.model.carton.news_add", args, null));
    }
    public news_has(args: any) {
        this.triggerEvent(new AppEvent("app.model.carton.news_has", args, null));
    }
    public user_login(args: any) {
        this.triggerEvent(new AppEvent("app.model.carton.user_login", args, null));
    }
}

export { CartonController };
